<template>


  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="申请单名称" prop="name">
        <el-select v-model="queryParams.name" placeholder="请选择申请单" clearable>
          <el-option
            v-for="dict in name_data"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="申请人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <!--审核通过或驳回-->
      <el-form-item label="申请单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择申请单状态" clearable>
          <el-option
            v-for="dict in dict.type.from_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请时间" prop="changTime">
        <el-date-picker
          clearable
          v-model="queryTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          placeholder="请选择抄送时间"
          @change="handleDatePickerChange">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:form:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:form:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:form:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:form:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请单名称" align="center" prop="name" />
      <el-table-column label="申请人" align="center" prop="userName" />
<!--      <el-table-column label="模板数据明细" align="center" prop="templateInfo" />-->
      <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="changeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="申请单相关人员(关联审批流程)" align="center" prop="processDetail" />-->
      <el-table-column label="申请单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.from_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleDetail(scope.row)"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-close"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:form:edit']"-->
<!--          >驳回</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-check"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:form:remove']"-->
<!--          >通过</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请单名称" prop="name">
          <el-input v-model="form.name" :disabled="true"/>
        </el-form-item>
        <el-form-item label="申请单状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.from_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="550px" append-to-body>
      <el-form ref="form" v-model="form" :rules="rules" label-width="100px">
        <el-form-item label="申请人" prop="username">
          <el-input v-model="form.username" readonly/>
        </el-form-item>
        <el-form-item label="抄送人" prop="copyUsername">
          <el-input v-model="form.copyUsername" readonly/>
        </el-form-item>
        <el-form-item label="审批人" prop="approvalUsername">
          <el-input v-model="form.approvalUsername" readonly/>
        </el-form-item>
        <el-form-item v-for="(item, index) in detailForm" :key="index" :label="item.dataAttribution" :prop="item.dataAttribution">
          <el-input v-model="item.dataContent" readonly />
        </el-form-item>
        <el-form-item v-model="img" :label="img.dataAttribution" :prop="img.dataAttribution" v-if="images" class="image-container">
          <template>
            <div class="image-container">
              <img v-for="(item, index) in images" :key="index" :src="item" alt="Vue Image" class="custom-image" />
            </div>
          </template>
        </el-form-item>
<!--        <el-form-item label="申请单名称" prop="name">-->
<!--          <el-input v-model="form.name" :disabled="true"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申请单状态" prop="status">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.from_status"-->
<!--              :key="dict.value"-->
<!--              :label="parseInt(dict.value)"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailSubmit">确 定</el-button>
        <el-button @click="detailCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listForm, getForm, delForm, addForm, updateForm, getFormDetail  } from "@/api/system/form";

export default {
  name: "Form",
  dicts: ['from_status'],
  data() {
    return {
      // 最终展示的图片数组
      name_data:[
        {value:"代理退款",label:"代理退款"},
        {value:"报销",label:"报销"},
        {value:"代理清授信",label:"代理清授信"},
        {value:"渠道下游预付款加款申请",label:"渠道下游预付款加款申请"},
        {value:"下游卡密出货",label:"下游卡密出货"},
        {value:"下游扣款申请（原手动扣款）",label:"下游扣款申请（原手动扣款）"},
        {value:"下游加款申请（原手动加款）",label:"下游加款申请（原手动加款）"},
        {value:"渠道上游预付款申请",label:"渠道上游预付款申请"},
        {value:"预付款收款登记",label:"预付款收款登记"},
        {value:"代理授信",label:"代理授信"},
        {value:"执行项目预付申请",label:"执行项目预付申请"},
        {value:"费用",label:"费用"},
        {value:"内部转账",label:"内部转账"},
        {value:"其它退款",label:"其它退款"},
        {value:"项目垫资申请",label:"项目垫资申请"},
        {value:"保证金申请",label:"保证金申请"},
        {value:"特殊款项付款",label:"特殊款项付款"},
        {value:"上游卡密付款申请",label:"上游卡密付款申请"},
        {value:"银行付款登记",label:"银行付款登记"},
        {value:"银行收款登记",label:"银行收款登记"},
        {value:"代理降授信(新)",label:"代理降授信(新)"},
        {value:"代理授信(新)",label:"代理授信(新)"},
        {value:"后付费付款",label:"后付费付款"},
      ],


      queryTime: null,
      images:[],
      openDetail: false,
      detailForm: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请表表格数据
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        templateInfo: null,
        creatTime: null,
        changeTime: null,
        processDetail: null,
        status: null,
        userName: null,
        startTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      img:{},
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleDatePickerChange(value) {
      if (value == null){
        this.queryParams.startTime =null;
        this.queryParams.endTime =null;
      } else {
        this.queryParams.startTime =value[0];
        this.queryParams.endTime =value[1];
      }
    },
    // 确定 detailSubmit
    detailSubmit() {
      this.openDetail = false;
      this.reset();
    },
    // 取消按钮
    detailCancel() {
      this.openDetail = false;
      this.reset();
    },
    /** 查询申请表列表 */
    getList() {
      this.loading = true;
      listForm(this.queryParams).then(response => {
        this.formList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        templateInfo: null,
        creatTime: null,
        changeTime: null,
        processDetail: null,
        status: null,
        userName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请表";
    },
    /** 查看单行详情 */
    handleDetail(row){
      this.reset();
      const id = row.id || this.ids
      getFormDetail(id).then(response => {
        this.form = response.data;
        this.detailForm = response.data.tdataInfos;
        this.img = this.detailForm.at(this.detailForm.length-1);
        var image = this.img.dataContent;
        if (image !== ""){
          this.images = this.img.dataContent.split(",");
        } else {
          this.images = undefined;
        }
        // this.detailForm = this.detailForm.slice(0, this.detailForm.length - 1);
        this.detailForm.pop();
        this.openDetail = true;
        this.title = "申请单详情表";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getForm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改申请表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除申请表编号为"' + ids + '"的数据项？').then(function() {
        return delForm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.queryParams.name==null||this.queryParams.name==''){
        this.$modal.msgError("由于不同申请单表头不同，导出必须选择申请单！");
        return ;
      }
        this.$confirm('导出默认为通过审批的记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.download('system/form/export', {
            ...this.queryParams
          }, `form_${new Date().getTime()}.xlsx`)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消导出操作'
          });
        });

    }
  }
};
</script>

<style>
.image-container {
  display: flex;
}

.custom-image {
  width: 100px; /* 调整图片宽度 */
  height: 100px; /* 调整图片高度 */
  margin-right: 10px; /* 可选：设置图片之间的右边距，使它们之间有间隙 */
}
</style>
